var Based = (function(Based) {
	/*|获取文件
	|*|
	|*/
	var load = function(url, method, names, callback) {
		var xmlhttp
		var names = names || ''
		if (typeof method === 'function') {
			callback = method
			method = 'GET'
		}
		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest()
		} else {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
		}
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				callback(xmlhttp)
			}
			else {
				console.log(xmlhttp)
			}
		}
		xmlhttp.open(method, url, true)
		xmlhttp.setRequestHeader('Accept','application/json, text/plain, */*')
		xmlhttp.send()
		//return `method: ${method}, url: ${url}, callback: ${callback}, xmlhttp: ${xmlhttp}`
	}
	/*|用 get 方法获取
	|*|
	|*/
	var get = function(url, names, callback) {
		if (typeof names === 'function') {
			callback = names
			names = {}
		}
		load(url, 'GET', names, callback)
	}
	/*|加载一个脚本
	|*|@param {string} url - 脚本的地址
	|*|@param {function} callback - 脚本加载完成时运行
	|*/
	var scripts = []
	var _import = function(url, callback) {
		var script = document.createElement('script')
		script.setAttribute('src', url)
		callback = !callback || script.addEventListener('load', callback)
		return script
	}
	var importStart = function(url, callback) {
		setTimeout(endImport, 0)
		importAll(url, callback)
		this.import = importAll
		return this
	}
	var importAll = function(url, callback) {
		try {
			scripts.push(_import(url, callback))
		} catch(e) {
			console.error(e)
		} finally {
			return this
		}
	}
	var endImport = function() {
		var length = scripts.length, i
		this.import = importStart
		if (length === 0) {
			return 0
		}
		function add(element) {
			return function(event) {
				document.head.appendChild(element)
			}
		}
		for(i = 0; i < length; i += 1) {
			if (scripts[i + 1]) {
				scripts[i].addEventListener('load', add(scripts[i + 1]))
			}
		}
		document.head.appendChild(scripts[0])
		scripts = []
	}
	var list = location.search
	/**
	 * 获取网页 GET 请求的参数
	 * @param  {string} name  参数名
	 * @param  {*}      could 可能的值
	 * @return {string}       参数值
	 */
	var greet = function(name, could) {
		var arr,
		reg = new RegExp(name + '=([^&]*)&?')
		if (arr = list.match(reg))
			return arr[1]
		else
			return ''
	}
	/**
	 * 判断参数是否设置
	 * @param  {string}  name 参数名
	 * @return {boolean}      存在性
	 */
	var isSet = function(name) {
		reg = new RegExp(name, 'g')
		if (!reg.test(list)) {
			return false
		}
		if (!greet(name) === '') {
			return false
		}
		return true
	}
	return {
		load: load,
		get: get,
		import: importStart,
		greet: greet,
		isSet: isSet,
	}
})(window.Based || {})
